<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="订单编号" prop="id">
            <el-input v-model="queryParams.id" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="服务人员电话" prop="serveProviderStaffPhone">
            <el-input v-model="queryParams.serveProviderStaffPhone" placeholder="请输入" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="订单状态">
            <el-select v-model="queryParams.ordersStatus" placeholder="请选择" clearable>
              <el-option label="请选择" value="" />
              <el-option label="待支付" value="0" />
              <el-option label="派单中" value="100" />
              <el-option label="待服务" value="200" />
              <el-option label="服务中" value="300" />
              <el-option label="待评价" value="400" />
              <el-option label="已完成" value="500" />
              <el-option label="已取消" value="600" />
              <el-option label="已关闭" value="700" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="交易结束时间" prop="transactionCompletionTime" label-width="100px">
          <el-date-picker v-model="daterangeTransactionCompletionTime" type="daterange" range-separator="-"
            start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" clearable />
        </el-form-item>
        <el-col :span="8">
          <el-form-item label="支付状态">
            <el-select v-model="queryParams.payStatus" placeholder="请选择" clearable>
              <el-option label="请选择" value="" />
              <el-option label="未支付" value="0" />
              <el-option label="已支付" value="1" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="退款状态">
            <el-select v-model="queryParams.refundStatus" placeholder="请选择" clearable>
              <el-option label="请选择" value="" />
              <el-option label="退款成功" value="1" />
              <el-option label="退款中" value="2" />
              <el-option label="退款失败" value="3" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>



    <el-table v-loading="loading" :data="HistoryOrdersList" @selection-change="handleSelectionChange"
      style="width: 100%" border :scrollx="true">
      <el-table-column label="订单id" width="120" align="center" prop="id" />
      <el-table-column label="服务名称" width="120" align="center" prop="serveItemName" />
      <el-table-column label="订单状态" align="center" prop="ordersStatus" width="100">
        <template slot-scope="scope">
          <span :class="formatOrderStatus(scope.row.ordersStatus)">
            {{ getOrderStatusText(scope.row.ordersStatus) }}
          </span>
        </template>
      </el-table-column>

      <el-table-column label="支付状态" align="center" prop="payStatus" width="140">
        <template slot-scope="scope">
          <span :class="formatPayStatus(scope.row.payStatus)">
            {{ getPayStatus(scope.row.payStatus) }}
          </span>
        </template>
      </el-table-column>

      <el-table-column label="退款状态" align="center" prop="refundStatus" width="140">
        <template slot-scope="scope">
          {{ formatRefundStatus(scope.row.refundStatus) }}
        </template>
      </el-table-column>
      <el-table-column label="订单金额" width="120" align="center" prop="totalAmount" />
      <el-table-column label="实付金额" width="120" align="center" prop="realPayAmount" />
      <el-table-column label="服务地址" width="" align="center" prop="serveAddress" />
      <el-table-column label="服务人员姓名" width="120" align="center" prop="serveProviderStaffName" />

      <!-- 以下列可以通过滚动查看 -->
      <el-table-column label="服务人员手机号" align="center" prop="serveProviderStaffPhone" />
      <el-table-column label="下单时间" align="center" prop="placeOrderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.placeOrderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易结束时间" align="center" prop="transactionCompletionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.transactionCompletionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="routerPush(scope.row.id)"
            v-hasPermi="['HistoryOrders:HistoryOrders:remove']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改历史订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单所属人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入订单所属人" />
        </el-form-item>
        <el-form-item label="服务类型id" prop="serveTypeId">
          <el-input v-model="form.serveTypeId" placeholder="请输入服务类型id" />
        </el-form-item>
        <el-form-item label="服务人" prop="serveProviderId">
          <el-input v-model="form.serveProviderId" placeholder="请输入服务人" />
        </el-form-item>
        <el-form-item label="服务项id" prop="serveItemId">
          <el-input v-model="form.serveItemId" placeholder="请输入服务项id" />
        </el-form-item>
        <el-form-item label="服务id" prop="serveId">
          <el-input v-model="form.serveId" placeholder="请输入服务id" />
        </el-form-item>
        <el-form-item label="城市编码" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入城市编码" />
        </el-form-item>
        <el-form-item label="服务类型名称" prop="serveTypeName">
          <el-input v-model="form.serveTypeName" placeholder="请输入服务类型名称" />
        </el-form-item>
        <el-form-item label="服务名称" prop="serveItemName">
          <el-input v-model="form.serveItemName" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item label="服务项图片" prop="serveItemImg">
          <el-input v-model="form.serveItemImg" placeholder="请输入服务项图片" />
        </el-form-item>
        <el-form-item label="服务单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入服务单位" />
        </el-form-item>
        <el-form-item label="订单完成时间" prop="tradeFinishTime">
          <el-date-picker clearable v-model="form.tradeFinishTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择订单完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付渠道" prop="tradingChannel">
          <el-input v-model="form.tradingChannel" placeholder="请输入支付渠道" />
        </el-form-item>
        <el-form-item label="三方支付渠道" prop="thirdOrderId">
          <el-input v-model="form.thirdOrderId" placeholder="请输入三方支付渠道" />
        </el-form-item>
        <el-form-item label="派单时间" prop="dispatchTime">
          <el-date-picker clearable v-model="form.dispatchTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择派单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="购买数量" prop="purNum">
          <el-input v-model="form.purNum" placeholder="请输入购买数量" />
        </el-form-item>
        <el-form-item label="订单金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="realPayAmount">
          <el-input v-model="form.realPayAmount" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="三方退款流水" prop="thirdRefundOrderId">
          <el-input v-model="form.thirdRefundOrderId" placeholder="请输入三方退款流水" />
        </el-form-item>
        <el-form-item label="优惠金额" prop="discountAmount">
          <el-input v-model="form.discountAmount" placeholder="请输入优惠金额" />
        </el-form-item>
        <el-form-item label="服务地址" prop="serveAddress">
          <el-input v-model="form.serveAddress" placeholder="请输入服务地址" />
        </el-form-item>
        <el-form-item label="联系人手机号" prop="contactsPhone">
          <el-input v-model="form.contactsPhone" placeholder="请输入联系人手机号" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactsName">
          <el-input v-model="form.contactsName" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="取消人" prop="cancelerName">
          <el-input v-model="form.cancelerName" placeholder="请输入取消人" />
        </el-form-item>
        <el-form-item label="服务人员姓名" prop="serveProviderStaffName">
          <el-input v-model="form.serveProviderStaffName" placeholder="请输入服务人员姓名" />
        </el-form-item>
        <el-form-item label="服务人员员手机号" prop="serveProviderStaffPhone">
          <el-input v-model="form.serveProviderStaffPhone" placeholder="请输入服务人员员手机号" />
        </el-form-item>
        <el-form-item label="机构名称" prop="institutionName">
          <el-input v-model="form.institutionName" placeholder="请输入机构名称" />
        </el-form-item>
        <el-form-item label="机构电话" prop="institutionPhone">
          <el-input v-model="form.institutionPhone" placeholder="请输入机构电话" />
        </el-form-item>
        <el-form-item label="下单时间" prop="placeOrderTime">
          <el-date-picker clearable v-model="form.placeOrderTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务开始时间" prop="serveStartTime">
          <el-date-picker clearable v-model="form.serveStartTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择服务开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务结束时间" prop="serveEndTime">
          <el-date-picker clearable v-model="form.serveEndTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择服务结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际服务开始时间" prop="realServeStartTime">
          <el-date-picker clearable v-model="form.realServeStartTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择实际服务开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际服务结束时间" prop="realServeEndTime">
          <el-date-picker clearable v-model="form.realServeEndTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择实际服务结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务开始说明" prop="serveBeforeIllustrate">
          <el-input v-model="form.serveBeforeIllustrate" placeholder="请输入服务开始说明" />
        </el-form-item>
        <el-form-item label="服务完成说明" prop="serveAfterIllustrate">
          <el-input v-model="form.serveAfterIllustrate" placeholder="请输入服务完成说明" />
        </el-form-item>
        <el-form-item label="支付超时时间，该时间只对待支付有意义" prop="paymentTimeout">
          <el-date-picker clearable v-model="form.paymentTimeout" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择支付超时时间，该时间只对待支付有意义">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经度" prop="lon">
          <el-input v-model="form.lon" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable v-model="form.payTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="取消/被退单时间" prop="cancelTime">
          <el-date-picker clearable v-model="form.cancelTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择取消/被退单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="取消/被退单原因" prop="cancelReason">
          <el-input v-model="form.cancelReason" placeholder="请输入取消/被退单原因" />
        </el-form-item>
        <el-form-item label="交易结束时间" prop="transactionCompletionTime">
          <el-date-picker clearable v-model="form.transactionCompletionTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择交易结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评价时间" prop="evaluationTime">
          <el-date-picker clearable v-model="form.evaluationTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择评价时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评分" prop="evaluationScore">
          <el-input v-model="form.evaluationScore" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="用户端是否展示，1：展示，0：隐藏" prop="display">
          <el-input v-model="form.display" placeholder="请输入用户端是否展示，1：展示，0：隐藏" />
        </el-form-item>
        <el-form-item label="排序时间字段" prop="sortTime">
          <el-date-picker clearable v-model="form.sortTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择排序时间字段">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
/* 添加滚动条样式 */
.app-container {
  height: 100vh;
  /* 根据实际情况调整高度 */
  overflow: auto;
  /* 添加滚动条 */
}

.el-table {
  width: auto !important;
  /* 确保表格宽度自动 */
}

.status-success::before {
  content: '•';
  color: #67c23a;
  /* 绿色文字 */
  display: inline-block;
  position: absolute;
  left: -20px;
  width: 20px;
  height: 20px;
  font-size: 23px;
  border-radius: 50%;
}

.status-cancel::before {
  content: '•';
  color: #f56c6c;
  /* 红色文字 */
  display: inline-block;
  position: absolute;
  left: -20px;
  width: 20px;
  height: 20px;
  font-size: 23px;
  border-radius: 50%;
}

.status-pending::before {
  content: '•';
  color: #e6a23c;
  /* 黄色文字 */
  display: inline-block;
  position: absolute;
  left: -20px;
  width: 20px;
  height: 20px;
  font-size: 23px;
  border-radius: 50%;
}

.status-success,
.status-pending,
.status-cancel {
  display: inline-block;
  position: relative;
  left: 6px;
  border-radius: 50%;
  text-align: center;
}
</style>
<script>
import { addHistoryOrders, getHistoryOrders, listHistoryOrders, updateHistoryOrders } from "@/api/historyOrders/historyOrders";

export default {
  name: "HistoryOrders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史订单表格数据
      HistoryOrdersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 排序时间字段时间范围
      daterangeTransactionCompletionTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        ordersStatus: null,
        payStatus: null,
        refundStatus: null,
        serveProviderStaffPhone: null,
        transactionCompletionTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "订单所属人不能为空", trigger: "blur" }
        ],
        serveItemId: [
          { required: true, message: "服务项id不能为空", trigger: "blur" }
        ],
        serveId: [
          { required: true, message: "服务id不能为空", trigger: "blur" }
        ],
        cityCode: [
          { required: true, message: "城市编码不能为空", trigger: "blur" }
        ],
        ordersStatus: [
          { required: true, message: "订单状态不能为空", trigger: "change" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
        purNum: [
          { required: true, message: "购买数量不能为空", trigger: "blur" }
        ],
        totalAmount: [
          { required: true, message: "订单金额不能为空", trigger: "blur" }
        ],
        realPayAmount: [
          { required: true, message: "实付金额不能为空", trigger: "blur" }
        ],
        discountAmount: [
          { required: true, message: "优惠金额不能为空", trigger: "blur" }
        ],
        serveAddress: [
          { required: true, message: "服务地址不能为空", trigger: "blur" }
        ],
        contactsPhone: [
          { required: true, message: "联系人手机号不能为空", trigger: "blur" }
        ],
        contactsName: [
          { required: true, message: "联系人姓名不能为空", trigger: "blur" }
        ],
        serveStartTime: [
          { required: true, message: "服务开始时间不能为空", trigger: "blur" }
        ],
        sortTime: [
          { required: true, message: "排序时间字段不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询历史订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeTransactionCompletionTime && '' != this.daterangeTransactionCompletionTime) {
        this.queryParams.params["beginTransactionCompletionTime"] = this.daterangeTransactionCompletionTime[0];
        this.queryParams.params["endTransactionCompletionTime"] = this.daterangeTransactionCompletionTime[1];
      }
      listHistoryOrders(this.queryParams).then(response => {
        this.HistoryOrdersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        serveTypeId: null,
        serveProviderId: null,
        serveProviderType: null,
        serveItemId: null,
        serveId: null,
        cityCode: null,
        serveTypeName: null,
        serveItemName: null,
        serveItemImg: null,
        unit: null,
        ordersStatus: null,
        payStatus: null,
        refundStatus: null,
        tradeFinishTime: null,
        tradingChannel: null,
        thirdOrderId: null,
        dispatchTime: null,
        price: null,
        purNum: null,
        totalAmount: null,
        realPayAmount: null,
        thirdRefundOrderId: null,
        discountAmount: null,
        serveAddress: null,
        contactsPhone: null,
        contactsName: null,
        cancelerName: null,
        serveProviderStaffName: null,
        serveProviderStaffPhone: null,
        institutionName: null,
        institutionPhone: null,
        placeOrderTime: null,
        serveStartTime: null,
        serveEndTime: null,
        realServeStartTime: null,
        realServeEndTime: null,
        serveBeforeImgs: null,
        serveBeforeIllustrate: null,
        serveAfterImgs: null,
        serveAfterIllustrate: null,
        paymentTimeout: null,
        lon: null,
        lat: null,
        payTime: null,
        cancelTime: null,
        cancelReason: null,
        transactionCompletionTime: null,
        evaluationTime: null,
        evaluationScore: null,
        display: null,
        sortTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeTransactionCompletionTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加历史订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHistoryOrders(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改历史订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHistoryOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHistoryOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }, getOrderStatusText(status) {
      const statusMap = {
        '0': '待支付',
        '100': '派单中',
        '200': '待服务',
        '300': '服务中',
        '400': '待评价',
        '500': '已完成',
        '600': '已取消',
        '700': '已关闭',
      };
      return statusMap[status] || '未知状态';
    },
    formatOrderStatus(status) {
      if (status == 500) {
        return 'status-success';
      } else if (status == 600) {
        return 'status-cancel';
      } else {
        return 'status-pending';
      }
    },
    // 支付状态映射
    getPayStatus(status) {
      const statusMap = {
        0: '未支付',
        1: '已支付',
        2: '支付中',
        4: '支付失败'
      };
      return statusMap[status] || '';
    },
    formatPayStatus(status) {
      if (status == 0) {
        return 'status-success';
      } else if (status == 4) {
        return 'status-cancel';
      } else {
        return 'status-pending';
      }
    },

    // 退款状态映射
    formatRefundStatus(status) {
      const statusMap = {
        0: '退款失败',
        1: '退款成功',
        2: '退款中'
      };
      return statusMap[status] || '';
    },



    //页面跳转
    routerPush(id) {
      this.$router.push({ name: 'historyOrderDetails', params: { id: id } });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('HistoryOrders/HistoryOrders/export', {
        ...this.queryParams
      }, `HistoryOrders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
